<?php
// +----------------------------------------------------------------------
// | NXCMS - 一个免费的多语言外贸企业网站系统
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.nxcms.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Version: v1.0.0
// +----------------------------------------------------------------------

/** +----------------------------------------------------------------------
 * | 后台语言管理功能
 * | 
// +----------------------------------------------------------------------
// | Author: Hippo <nxcms.cn@gmail.com> 
// +----------------------------------------------------------------------
// | Date: 2016/7/2
// +----------------------------------------------------------------------
// | Version: v1.0.0
// +----------------------------------------------------------------------
*/

class LanguageController extends AdminController{

// 	public function indexAction(){
				
// 		$this->display('language_index.html');
// 	}

	// 添加语言
	public function addAction()
	{	
		$error = $name_erorr = $lang_code_error = '';
		if($_POST){
			$name = isset($_POST['name'])? trim($_POST['name']): '';
			$lang_code = isset($_POST['lang_code'])? trim($_POST['lang_code']): '';
			if(!$name){
				$name_erorr = '语言名称不可以为空！';
			}elseif(!$lang_code){
				$lang_code_error = '语言代码不可以为空！';
			}else{
				$data = array();
				$data['name'] = $name;
				$data['lang_code'] = $lang_code;
				$model = new Model();
				$rs = $model->insert('language', $data);				
				
				if($rs){
					header('location:/admin/index.php?c=language');
				}else{
					$error = '添加语言失败！';
				}
			}
		}
		
		

		$this->assign('error', $error);
		$this->assign('name_erorr', $name_erorr);
		$this->assign('lang_code_error', $lang_code_error);
		$this->display('language-add.html');
	}
	
	// 列表
	public function indexAction()
	{

		$model = new Model();

		$datas = $model->select('language', ['id','name','lang_code']);

		foreach($datas as $val){
			$name=$val['name'];
			$lang_code=$val['lang_code'];					
		}
	
		$this->assign('datas',$datas);
		$this->display('language-index.html');
	}
	
	// 删除操作
	public function deleteAction()
	{
		$id = isset($_GET['id'])? trim($_GET['id']): '';
		$model = new Model();		
		$model->delete("language", ['id'=>$id]);	
		header('location:/admin/index.php?c=language');
	}
	
	// 修改
	public function updateAction()
	{
		$id = isset($_GET['id'])? trim($_GET['id']): '';		
		//  有提交时，执行数据更新
		if($_POST){
			$name = isset($_POST['name'])? trim($_POST['name']): '';
			$lang_code = isset($_POST['lang_code'])? trim($_POST['lang_code']): '';
			if(!$name){
				$name_erorr = '语言名称不可以为空！';
			}elseif(!$lang_code){
				$lang_code_error = '语言代码不可以为空！';
			}else{
				$data = array();
				$data['name'] = $name;
				$data['lang_code'] = $lang_code;
				$model = new Model();
	
				$rs=$model->update('language',$data,["id"=>$id]);
				if($rs){
					header('location:/admin/index.php?c=language&a=index');
				}else{
					$error = '添加语言失败！';
				}	
			}
			
		}else{
			$id = isset($_GET['id'])? trim($_GET['id']): '';
			$model = new Model();
			$language = $model->get('language', ['id','name','lang_code'],["id" => $id]);
			//print_r($language);
			$this->assign('language',$language);
			
		}
	
		$this->display('language-update.html');
	}

}